package com.ext.aic.components;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.Drawable;
import android.view.animation.LinearInterpolator;

public class AnimatedGradientDrawable extends Drawable implements Animatable {
    private final int[] colors = {0xE63FB2FF, 0xE6EAE1FF, 0xE675FFDF};
    private final float[] positions = {0f, 0.5f, 1f};
    private final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private float gradientOffset = 0f;
    private ObjectAnimator animator;
    private Rect bounds;
    private float cornerRadius = 0f; // 新增圆角半径属性

    public AnimatedGradientDrawable() {
        paint.setDither(true);
        setupAnimator();
    }

    // 新增圆角设置方法
    public void setCornerRadius(float radius) {
        this.cornerRadius = radius;
        invalidateSelf();
    }

    private void setupAnimator() {
        animator = ObjectAnimator.ofFloat(this, "gradientOffset", 0, 1);
        animator.setDuration(2000);
        animator.setInterpolator(new LinearInterpolator());
        animator.setRepeatCount(ValueAnimator.INFINITE);
    }

    @Override
    protected void onBoundsChange(Rect bounds) {
        super.onBoundsChange(bounds);
        this.bounds = new Rect(bounds);
        updateGradient();
    }

    private void updateGradient() {
        if (bounds == null) return;

        float width = bounds.width();
        float height = bounds.height();

        float diagonal = (float) (Math.hypot(width, height) * 1.2);
        float startX = -diagonal + (diagonal * 2 * gradientOffset);
        float startY = -diagonal + (diagonal * 2 * gradientOffset);

        LinearGradient gradient = new LinearGradient(
                startX,
                startY,
                startX + diagonal,
                startY + diagonal,
                colors,
                positions,
                Shader.TileMode.MIRROR);

        paint.setShader(gradient);
        invalidateSelf();
    }

    public void setGradientOffset(float offset) {
        gradientOffset = offset;
        updateGradient();
    }

    @Override
    public void draw(Canvas canvas) {
        if (bounds != null) {
            // 修改绘制方法为圆角矩形
            RectF rect = new RectF(bounds);
            canvas.drawRoundRect(rect, cornerRadius, cornerRadius, paint);
        }
    }

    @Override
    public void setAlpha(int alpha) {
        paint.setAlpha(alpha);
    }

    @Override
    public void setColorFilter(ColorFilter colorFilter) {
        paint.setColorFilter(colorFilter);
    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }

    @Override
    public void start() {
        if (!animator.isRunning()) {
            animator.start();
        }
    }

    @Override
    public void stop() {
        if (animator.isRunning()) {
            animator.end();
        }
    }

    @Override
    public boolean isRunning() {
        return animator.isRunning();
    }
}
